package com.hd.assistant.web.common.base.ordermining;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.hd.assistant.biz.manager.AssistantManager;
import com.hd.assistant.biz.manager.exception.ManagerException;
import com.hd.assistant.biz.manager.ordermining.ItemCostCacheManager;
import com.hd.assistant.biz.manager.ordermining.OrderMiningManager;
import com.hd.assistant.dal.dataobject.User;
import com.hd.assistant.web.common.display.IsvOrderInfo;
import com.hd.common.taobao.DefaultTaobaoISVAppManager;
import com.taobao.api.ApiException;
import com.taobao.api.response.VasSubscribeGetResponse;



public class BaseTaobaoOrderModule {
	public static String APP_TYPE= "appType";
	 private final static Logger log = LoggerFactory.getLogger(BaseTaobaoOrderModule.class);
	
	@Autowired
	protected AssistantManager assistantManager;
	

	@Autowired
	protected  DefaultTaobaoISVAppManager taobaoISVAppManager ;
	
	@Autowired
	protected OrderMiningManager orderMiningManager;
	
	@Autowired
	protected ItemCostCacheManager itemCostCacheManager;
	
	protected static String TAOBAO_SESSION_KEY = "TU_1";
	protected User checkLogin(HttpSession session,String type){
		if(type.equals(User.Type_TAOBAO_ORDER)){
			Object o = session.getAttribute(TAOBAO_SESSION_KEY);
			User u = (User)o;
			return u;
		}
		return null;
	}

	protected void supplyUserInfo(User u) throws ApiException{
		com.taobao.api.domain.User user = this.taobaoISVAppManager.getCurrentUserInfo(this.taobaoISVAppManager.getOrderAppKey(), this.taobaoISVAppManager.getOrderAppSecret(), u.getToken());
		u.setType(user.getType());
		u.setUserCredit(user.getSellerCredit());
	}

	
	protected User getUserInfo(Long uid,String type) throws ManagerException {
		return assistantManager.getUserByUserIdAndType(uid, type);
	}
	
	protected void storeUserInfo(User u) throws ManagerException {
			assistantManager.insertUser(u);
		
	}
	
	protected void updateUserInfo(User u) throws ManagerException {
			assistantManager.updateUser(u);
			itemCostCacheManager.updateOrderMiningUser(u.getUserId());
	}
	
	public String getAppType(HttpSession session){
		//return (String)session.getAttribute(APP_TYPE);
		return User.Type_TAOBAO_ORDER;
	}
	protected User checkPermision(String appType, HttpSession session,Context context,Navigator nav) {
    	if(appType == null)
    		appType = getAppType(session);
    	context.put(APP_TYPE,appType);
    	
    	
    	if(appType!=null  && appType.equals(User.Type_TAOBAO_ORDER)){
    		User user = checkLogin(session,User.Type_TAOBAO_ORDER);
        	if(user == null){
        		nav.redirectToLocation(this.taobaoISVAppManager.getRedirectOrderAuth());
        	}else{
        		context.put("user", user);
        		try {
					VasSubscribeGetResponse res = taobaoISVAppManager.getVasSubscribeInfo(taobaoISVAppManager.getOrderAppKey(),
							taobaoISVAppManager.getOrderAppSecret(), user.getUserNick(), IsvOrderInfo.ORDERMINING_ISV_ARTICLECODE);
					if(res != null){
						IsvOrderInfo isvInfo = new IsvOrderInfo();
						isvInfo.setArticles(res.getArticleUserSubscribes());
						context.put("isvInfo", isvInfo);
					}
				} catch (ApiException e) {
					log.error("taobaoISVAppManager.getVasSubscribeInfo",e);
				}
            			
        		return user;
        	}
    	}
    	return null;
	}
	
	

}
